$(function() {

  function zerofill(n) {
    return n < 10 ? '0' + n : n;
  }

  initData();

  function initData() {
    $.ajax({
      url: '/qfang-weixin/qiye/meeting/myMeeting',
      data: {
        mobile: $.config.mobile,
        type: 'YES'
      },
      beforeSend: function() {
        $.showPreloader();
      },

      success: function(data) {
        $.hidePreloader();
        if (data.code == '1000') {
          var html = '';
          var items = data.data.items;
          var className = '';
          if (items.length) {
            $.each(items, function(k, v) {
              var time = null;
              if (!v.title) {
                className = 'update-data';
                v.title = '';
              } else {
                className = '';
              }

              var newDate = new Date(v.date);
              var timeText = null;
              var startTime = v.dates.split(',')[0].split('-')[0];

              html += '<li data-id="' + v.id + '" class="' + className + '">' +
                '<div class="row">' +
                '<span class="col-33 field-name">会议主题:</span>' +
                '<span><b>' + v.title + '</b></span>' +
                '</div>' +
                '<div class="row">' +
                '<span class="col-33 field-name">开始时间:</span>' +
                '<span class="time">' + zerofill((newDate.getMonth() + 1)) + '月' + zerofill(newDate.getDate()) + '日 ' + startTime + ':00</span>' +
                '</div>' +
                '<div class="row">' +
                '<span class="col-33 field-name">会议时长:</span>' +
                '<span>' + v.dates.split(',').length + '  小时</span>' +
                '</div>' +
                '<div class="row">' +
                '<span class="col-33 field-name">会议地点:</span>' +
                '<span>' + v.storey + '  楼' + v.conferenceName + '</span>' +
                '</div>' +
                '<p class="cancellation"><a href="#" class="button button-big">退订</a></p>' +
                '</li>';
            });

            $('.no-data').addClass('dn');
            $('#bookingList').empty().append(html);
          } else {
            $('.no-data').removeClass('dn');
            $('#bookingList').empty();
          }
        }
      }
    });
  }

  $('#bookingList').on('click', 'li', function(e) {
    if ($(e.target).hasClass('cancellation') || $(e.target).hasClass('button-big')) {
      var self = $(e.target);
      $.confirm('确定退订?', function() {
        $.ajax({
          url: '/qfang-weixin/qiye/meeting/delete',
          data: { id: self.parents('li').data('id') },
          beforeSend: function() {
            self.prop('disabled', true);
            $.showPreloader();
          },

          success: function(data) {
            if (data.code == '1000') {
              setTimeout(initData(), 1500);
            } else {
              $.toast(data.msg);
              self.prop('disabled', false);
            }
          },

          complete: function() {
            $.hidePreloader();
          }
        });
      });

      return false;
    }

    if ($(this).hasClass('update-data')) {
      location.href = '/booking-meeting.html?id=' + $(this).data('id') + '';
    }
  });

});
